package com.wt.struggle.goodbye.user

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.MainActivity
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.db.DbUtil
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.sq.activity.PickDetailsActivity
import com.wt.struggle.goodbye.user.activity.*
import com.wt.struggle.goodbye.user.adapter.UserTitleAdapter
import com.wt.struggle.goodbye.view.CustomPop
import com.xin.lv.yang.utils.activity.PictureSelectorActivity
import com.xin.lv.yang.utils.utils.FileUtils
import com.xin.lv.yang.utils.utils.ImageUtil
import com.xin.lv.yang.utils.utils.StatusBarUtil
import kotlinx.android.synthetic.main.user_fragment_layout.*

class UserFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {

            Send.GET_USER_INFO -> {

                if (userRefreshView != null) {
                    userRefreshView.isRefreshing = false
                }

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val userInfo: ProUserInfo = gson!!.fromJson(result, ProUserInfo::class.java)
                        showUserInfo(userInfo)

                    }
                }
            }
        }
    }


    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.user_fragment_layout, container, false)
    }

    var dbUtil: DbUtil? = null

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        dbUtil = DbUtil(activity!!)

        initAdapter()
        initClick()

    }

    override fun onResume() {
        super.onResume()

        val uid = Share.getId(activity!!)

        if (uid != 0) {

            noLoginLinear.visibility = View.GONE
            finishLoginLinear.visibility = View.VISIBLE

            getUserInfo()

        } else {
            noLoginLinear.visibility = View.VISIBLE
            finishLoginLinear.visibility = View.GONE

        }

    }


    /**
     * 获取用户信息
     */
    private fun getUserInfo() {

        val uid = Share.getId(activity!!)

        log("----------$uid")

        send!!.getMyUserInfo(uid, handler)

    }

    var userInfo: ProUserInfo? = null


    /**
     * 显示用户信息
     */
    fun showUserInfo(userInfo: ProUserInfo) {

        this.userInfo = userInfo

        val avatar = userInfo.avatar

        ImageUtil.getInstance()
            .loadCircleImage(activity, userHead, Config.GET_IMAGE_URL + avatar, R.drawable.avatar_default)
        userName.text = userInfo.nickName
        tvUserPhone.text = userInfo.mobile

        tvCollectNum.text = (userInfo.numberOfContentCollected!!).toString()

        tvFouceNum.text = userInfo.numberOfUserCollected

        tvFansNum.text = userInfo.numberOfFans.toString()

        tvCycleNum.text = userInfo.numberOfCircleCollected.toString()

        val info = ProUserInfo()

        info.nickName = userInfo.nickName
        info.avatarurl = Config.GET_IMAGE_URL + avatar
        info.account = Share.getAccount(activity!!)

        dbUtil!!.update(info)


    }


    private fun initClick() {

        userRefreshView.setOnRefreshListener {

            getUserInfo()

            handler!!.postDelayed(Runnable {

                if (userRefreshView != null) {
                    userRefreshView.isRefreshing = false
                }

            }, 2000)
        }

        userHead.setOnClickListener {

            if (Share.getToken(activity!!) != "") {
                val intent = Intent(activity!!, EditUserInfoActivity::class.java)
                intent.putExtra("userInfo", userInfo)
                startActivity(intent)

            } else {

                startActivity(Intent(activity!!, LoginActivity::class.java))

            }

        }

        noLoginLinear.setOnClickListener {
            startActivity(Intent(activity!!, LoginActivity::class.java))
        }

        tvLoginTi.setOnClickListener {
            startActivity(Intent(activity!!, LoginActivity::class.java))
        }

        imageSet.setOnClickListener {
            val intent = Intent(activity!!, SetActivity::class.java)
            intent.putExtra("userInfo", userInfo)
            startActivity(intent)
        }

        collLinear.setOnClickListener {
            if (Share.getToken(activity!!) != "") {
                startActivity(Intent(activity!!, CollListActivity::class.java))
            } else {
                startActivity(Intent(activity!!, LoginActivity::class.java))
            }
        }

        focusOnLinear.setOnClickListener {
            if (Share.getToken(activity!!) != "") {
                val intent = Intent(activity!!, FocusOnActivity::class.java)
                intent.putExtra("code", 1)
                startActivity(intent)
            } else {
                startActivity(Intent(activity!!, LoginActivity::class.java))
            }

        }

        fenSiLinear.setOnClickListener {

            if (Share.getToken(activity!!) != "") {

                val intent = Intent(activity!!, FansActivity::class.java)

////                intent.putExtra("userInfo", userInfo)

                startActivity(intent)

            } else {
                startActivity(Intent(activity!!, LoginActivity::class.java))
            }

        }

        friendLinear.setOnClickListener {
            if (Share.getToken(activity!!) != "") {
                startActivity(Intent(activity!!, FriendManagerActivity::class.java))
            } else {
                startActivity(Intent(activity!!, LoginActivity::class.java))
            }

        }
    }


    val titleList = arrayListOf<String>(
        "我的相册", "分享转载",
        "浏览历史", "相关帖子",
        "删除回收站"
    )

    val picIntList = arrayListOf<Int>(
        R.drawable.user_2, R.drawable.user_3,
        R.drawable.user_4, R.drawable.user_5,
        R.drawable.user_7
    )


    private fun initAdapter() {

        userTitleRecycler.layoutManager = LinearLayoutManager(activity)
        val adapter = UserTitleAdapter(activity!!, titleList, picIntList, 1)
        userTitleRecycler.adapter = adapter

        adapter.itemClickListener = object : ItemClickListener {

            override fun onItemClick(position: Int) {

                if (Share.getToken(activity!!) != "") {

                    when (position) {

                        0 -> {
                            startActivity(Intent(activity!!, MyPhotoActivity::class.java))
                        }

                        1 -> {
                            startActivity(Intent(activity!!, ShareListActivity::class.java))
                        }

                        2 -> {
                            startActivity(Intent(activity!!, ScanHistoryActivity::class.java))
                        }
                        3 -> {
                            startActivity(Intent(activity!!, AboutInvitationActivity::class.java))
                        }

                        4 -> {
                            startActivity(Intent(activity!!, DeleteRecycleActivity::class.java))
                        }

                    }
                } else {
                    startActivity(Intent(activity!!, LoginActivity::class.java))
                }

            }

            override fun onLongClick(position: Int) {

            }

        }

    }
}